package com.banzhi.shop.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.banzhi.shop.R;
import com.banzhi.shop.activity.CommodityDetailsActivity;
import com.banzhi.shop.activity.CommodityDetails_DZZQActivity;
import com.banzhi.shop.adapter.CommonAdapter;
import com.banzhi.shop.entity.GoodsDetails.GoodsDetails_Data;
import com.banzhi.shop.entity.GoodsDetails.pinfo_data;
import com.banzhi.shop.model.CallBack.CommodityDetailsCallBack;
import com.banzhi.shop.model.ICommodityDetailsModel;
import com.banzhi.shop.model.Impl.CommodityDetailsImpl;
import com.banzhi.shop.utils.NoTokenUtils;
import com.banzhi.shop.utils.ToastUtils;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品详情页里的商品参数
 */

public class GoodsParameterFrg extends Fragment implements CommodityDetailsCallBack {
    public static final String TAG = "GoodsParameterFrg";
    private View view;
    private RecyclerView recyclerView;
    private ICommodityDetailsModel model;
    private Activity activity;
    private String GoodID;
    /**
     * 商品详情数据实体类
     */
    private GoodsDetails_Data goodsdata;
    private List<pinfo_data> pinfo_datas = new ArrayList<>();
    private CommonAdapter<pinfo_data> adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.frg_goos_parameter, container, false);
        initView();
        model = new CommodityDetailsImpl();
        activity = getActivity();
        if (activity instanceof CommodityDetailsActivity) {
            GoodID = ((CommodityDetailsActivity) activity).getGoodID();
        }else if(activity instanceof CommodityDetails_DZZQActivity){
            GoodID = ((CommodityDetails_DZZQActivity) activity).getGoodID();
        }

        model.getData(GoodID, "1", this);
        return view;
    }


    private void initView() {
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
    }

    /**
     * 展示数据
     */
    private void showview() {

        if (adapter == null) {
            adapter = new CommonAdapter<pinfo_data>(getContext(), R.layout.item_canshu, pinfo_datas) {
                @Override
                public void convert(ViewHolder holder, pinfo_data pinfo_data, int position) {
                    holder.setText(R.id.tv_k, pinfo_data.getTitle());
                    holder.setText(R.id.tv_v, pinfo_data.getValue());
                }
            };
            recyclerView.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void ongetDataSuccess(Object object) {
        goodsdata = (GoodsDetails_Data) object;
        List<pinfo_data> data = goodsdata.getPinfo();
        pinfo_datas.clear();
        pinfo_datas.addAll(data);
        showview();
    }

    @Override
    public void ongetDataError(Object object) {
        JSONObject jsonObject = (JSONObject) object;
        NoTokenUtils.loginOut(getActivity(), jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
    }
}
